package com.ophiux.cs.application.chat.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ophiux.cs.application.chat.dto.UndoCSMessageRequest;
import com.ophiux.cs.application.chat.dto.UndoCSMessageResponse;
import com.ophiux.cs.application.chat.service.IUndoCSMessageAppService;
import com.ophiux.cs.application.core.Result;
import com.ophiux.cs.domain.chat.model.entity.CSChatMessageSend;
import com.ophiux.cs.domain.chat.repositories.ICSChatMessageSendRepository;
import com.ophiux.utils.exception.OphiuxRepositoryException;

/**
 * 撤销消息
 * @author chenh
 *
 */
@Service("undoCSMessageAppService")
public class UndoCSMessageAppService implements IUndoCSMessageAppService{

	@Autowired
	private ICSChatMessageSendRepository cSChatMessageSendRepository;
	
	@Override
	public Result<UndoCSMessageResponse> UndoChatMessage(UndoCSMessageRequest request) {
		
		CSChatMessageSend csmr = null;
		
		/**参数合法性验证**/
		if(!request.isParamValid()) {
			return Result.fail("", "参数不合法");
		}
		
		try {
			String msgID = request.getMSG_ID();	//消息内容ID
			
			csmr = cSChatMessageSendRepository
					.getCSChatMessageSendByMsgID(msgID);
			if(csmr == null) {
				return Result.fail("", "消息不存在");
			}
			csmr.undo();
			cSChatMessageSendRepository.save(csmr);
		} catch (OphiuxRepositoryException e) {
			e.printStackTrace();
		}
		
		return Result.success();
	}

}
